package br.srv.full.virtual.faces.manager.userLogin;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.richfaces.model.selection.Selection;

import br.srv.full.serviceLocator.ServiceLocator;
import br.srv.full.virtual.engine.users.PersonsManager;
import br.srv.full.virtual.engine.users.PersonsManagerException;
import br.srv.full.virtual.entities.auth.Role;
import br.srv.full.virtual.entities.auth.User; 
import br.srv.full.virtual.entities.resume.UserLogin;
import br.srv.full.virtual.faces.manager.Action;

/**
 * @author Delfino
 * 
 */
@TransactionManagement(TransactionManagementType.CONTAINER)
public final class UserRecordsManager {

	/**
	 * @author Delfino
	 * 
	 */
	public enum ActionLoginManager implements Action {
		/**
		 * 
		 */
		saveUserFailure,

		/**
		 * 
		 */
		saveUserSucess,

		/**
		 * 
		 */
		saveClientFailure
	}

	/**
	 * 
	 */
	private static final SelectItem SELECT_ITEM_NAO = new SelectItem(false, "Não");

	/**
	 * 
	 */
	private static final SelectItem SELECT_ITEM_SIM = new SelectItem(true, "Sim");

	private final Log log = LogFactory.getLog(getClass());

	/**
	 * 
	 */
	private LoginManager loginManager;
 

	private PersonsManager personManager;

	/**
	 * 
	 */
	private User user;

	private Selection selectedWork;
 

	private Selection selectedClient;

	/**
	 * 
	 */
	private WorkSelectionManager workSelectionManager;

 

	/**
	 * @return the loginManager
	 */
	public LoginManager getLoginManager() {
		return loginManager;
	}

	/**
	 * @param p_loginManager
	 *            the loginManager to set
	 */
	public void setLoginManager(LoginManager p_loginManager) {
		loginManager = p_loginManager;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		if (user == null)
			updateUser();
		return user;
	}

	/**
	 * 
	 */
	private void updateUser() {
		try {
			UserLogin l_login = getLoginManager().getUserLogin();
			user = l_login.getUser();
			if (user == null) {
				l_login = getPersonManager().refreshUserLogin(l_login);
				getLoginManager().setUserLogin(l_login);
				 user = l_login.getUser();

			}
		} catch (Exception e) {
			log.warn("Problemas ao Atualizar o User: " + user, e);
		}
	}

	/**
	 * @param p_user
	 *            the user to set
	 */
	public void setUser(User p_user) {
		user = p_user;
	}

	/**
	 * Salva os dados basicos do Usuário, e retorna qual página dever seguir.
	 * 
	 * @return
	 */
	public Action saveBaseUserRecord() {
		try {
			user = (User) getPersonManager().update(user);
		} catch (PersonsManagerException e) {
			log.warn("Problemas ao salvar o usuário!", e);
			return ActionLoginManager.saveUserFailure;
		}
		getLoginManager().setUser(user);
		return ActionLoginManager.saveUserSucess;
	}

	/**
	 * Restora os dados Basicos do Usuário antes de ter salvo,
	 * 
	 * @return
	 */
	public String restoreBaseUserRecord() {
		return null;
	}
 

 

	public List<SelectItem> getSelectItensYesNo() {
		List<SelectItem> l_itens = new ArrayList<SelectItem>();

		l_itens.add(SELECT_ITEM_NAO);
		l_itens.add(SELECT_ITEM_SIM);

		return l_itens;
	}

 

	/**
	 * @return
	 */
	private PersonsManager getPersonManager() {
		if (personManager == null) {
			personManager = ServiceLocator.getService(PersonsManager.class);
		}

		return personManager;
	}

	/**
	 * @return the selectedClient
	 */
	public Selection getSelectedClient() {
		return selectedClient;
	}

	/**
	 * @return the workSelectionManager
	 */
	public WorkSelectionManager getWorkSelectionManager() {
		return workSelectionManager;
	}

	/**
	 * @param p_workSelectionManager
	 *            the workSelectionManager to set
	 */
	public void setWorkSelectionManager(WorkSelectionManager p_workSelectionManager) {
		workSelectionManager = p_workSelectionManager;
	}
}
